#!/bin/bash

# This macro is needed to start the Root macros used for automatic testing
# from inside CMake using the add_test functionality. Since the tests
# starts with a fresh environment on has to set first the correct environment
# needed to run FairRoot.
# Also parameters defined in add_test will be converted in the correct format
# to be passed to root.

# Setup the needed environment
export LD_LIBRARY_PATH=@MY_LD_LIBRARY_PATH@:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=@MY_LD_LIBRARY_PATH@:$DYLD_LIBRARY_PATH
export PATH=@SIMPATH@/bin:$PATH:@Geant4_DIR@
export ROOTEXE=@ROOT_EXECUTABLE@
export ROOT_INCLUDE_PATH=@Geant3_INCLUDE_DIRS@:@MY_ROOT_INCLUDE_PATH@
export FAIRROOTPATH="@FAIRROOTPATH@"
export VMCWORKDIR=@VMCWORKDIR@
export Geant4VMC_MACRO_DIR=@Geant4VMC_MACRO_DIR@
export USE_VGM=1
export BOOST=@Boost_Avail@

@Geant4Data_Variables_sh@

export G3SYS="@Geant3_SYSTEM_DIR@"
export PYTHIA8DATA="@PYTHIA8DATA@"
if [ -e @Geant4_DIR@/env.sh ]; then
  . @Geant4_DIR@/env.sh
fi

# Concatenate all parameters given on the command line to one
# comma separated string
parameters=""
for i in $*; do
  if [ -z $parameters ]; then
    parameters=$i
  else
    parameters=$(echo "$parameters,$i")
  fi
done

cd @new_path@

# execute root with the defined macro and the command line parameters
$ROOTEXE -q -b @my_script_name@\($parameters\)
